package com.atguigu.java;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

/**
 * jdk 8中时间日期API的测试
 */
public class JDK8DateTimeTest {

    @Test
    public void testDate() {
        //偏移量
        Date date1 = new Date(2022 - 1900, 6 - 1, 9);
        System.out.println(date1);//Thu Jun 09 00:00:00 CST 2022
    }
    /*
    LocalDate、LocalTime、LocalDateTime的使用
    说明：
        1.localDatetime相较于LocalDate、LocalTime，使用频率要高
        2.类似于calendar

     */

    @Test
    public void test1() {
        //now()：获取日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():设定指定的年，月，日，是，分，秒，没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 46);
        System.out.println(localDateTime1);

        //getXxx():获取相关属性
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());

        //体现不可变性
        //withXxx():设置相关属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate1);//2022-09-22
        System.out.println(localDate);//2022-09-07

        LocalDateTime localDateTime2 = localDateTime.withHour(10);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        //不可变性
        //plus...():加相应的时间
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        //minus...():减相应的时间
        LocalDateTime localDateTime4 = localDateTime.minusDays(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);
    }

    /*
    Instant的使用：
    类似于java.util.Date类
     */
    @Test
    public void test2() {
        //now():获取本初子午线对应的标准时间
        Instant instant = Instant.now();
        System.out.println(instant);//2022-09-08T07:41:41.897Z

        //添加时间的偏移量
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);//2022-09-08T15:44:29.572+08:0

        //toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数
        //类似Date.getTime()
        long Milli = instant.toEpochMilli();
        System.out.println(Milli);//1662623258515

        //ofEpochMilli():通过给定的毫秒数获取Instance实例 ---> Date(long millis)
        Instant instant1 = Instant.ofEpochMilli(1662623258515L);
        System.out.println(instant1);//2022-09-08T07:47:38.515Z
    }
      /*
        DateTimeFormatter:格式化解析日期、时间
        类似于SimpleDateFormat
       */

    @Test
    public void test3() {
//     方式一：预定义的标准格式。如：ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIM：
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化：日期 ---> 字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String str = formatter.format(localDateTime);
        System.out.println(str);
        System.out.println(localDateTime);

        //解析：字符串 ---> 日期
        TemporalAccessor parse = formatter.parse(str);
        System.out.println(parse);//{},ISO resolved to 2022-09-08T16:10:50.693


//     方式二：本地化相关的格式。如：ofLocalizedDateTime(FormatStyle.LONG)
//     FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        //格式化：
        String str1 = formatter1.format(localDateTime);
        System.out.println(str1);//2022-9-8 16:23:01


//     本地化相关的格式。如：ofLocalizedDate()
//     FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
        DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
        //格式化：
        String str2 = formatter2.format(LocalDateTime.now());
        System.out.println(str2);//2022-9-8

//     重点：方式三：自定义的格式。如：ofPattern(“yyyy-MM-dd hh:mm:ss”)
        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
        //格式化：
        String str3 = formatter3.format(localDateTime.now());
        System.out.println(str3);//2022-09-08 04:28:28

        //解析：
        TemporalAccessor parse1 = formatter3.parse(str3);
        System.out.println(parse1);
//{NanoOfSecond=0, HourOfAmPm=4, MilliOfSecond=0, MinuteOfHour=29, SecondOfMinute=57, MicroOfSecond=0},ISO resolved to 2022-09-08
    }
}

